package com.cachopoid.utils;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.cachopoid.R;
import com.cachopoid.activity.DetalleActivity;
import com.cachopoid.vo.CachopoVO;

public class ListaExpandableCachoposBaseAdapter extends BaseExpandableListAdapter  {
	 
	private static ArrayList<CachopoVO> cachopoArrayList;
	private LayoutInflater l_Inflater;
	Context contextGlobal=null;
	//private String[] groups = { "Parent1", "Parent2","Parent3" };
    private String[][] children = { { "Child1" },{ "Child2" }, { "Child3" },{ "Child4" }, { "Child5" } };
	 
	public ListaExpandableCachoposBaseAdapter(Context context, ArrayList<CachopoVO> results) {
		cachopoArrayList = results;
	  l_Inflater = LayoutInflater.from(context);
	  contextGlobal=context;
	}
	 
	public int getCount() {
	  return cachopoArrayList.size();
	}
	 
	public Object getItem(int position) {
	  return cachopoArrayList.get(position);
	}
	 
	public long getItemId(int position) {
	  return position;
	}
	 

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return (CachopoVO)cachopoArrayList.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		CachopoVO cachopoInicial = new CachopoVO();
		final CachopoVO cachopo = (CachopoVO) getChild(groupPosition, childPosition);
		Utils.copyPropertiesCachopo(cachopo, cachopoInicial);
		
		
		final RatingBar tamanioBar;
		final TextView txtTamanio;
		final RatingBar rellenoBar;
		final TextView  txtRelleno;
		final RatingBar rebozadoBar;
		final TextView  txtRebozado;
		final RatingBar saborBar;
		final TextView  txtSabor;
		final RatingBar precioBar;
		final TextView  txtPrecio;
		if (convertView == null) {
			LayoutInflater l_Inflater = (LayoutInflater) contextGlobal.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = l_Inflater.inflate(R.layout.template_cachopos_detalle, null);
			
		    tamanioBar = (RatingBar) convertView.findViewById(R.id.tamanioBar);
		    txtTamanio = (TextView) convertView.findViewById(R.id.txtTamanio);
		    rellenoBar = (RatingBar)convertView.findViewById(R.id.rellenoBar);
			txtRelleno = (TextView)convertView.findViewById(R.id.txtRelleno);
			rebozadoBar = (RatingBar)convertView.findViewById(R.id.rebozadoBar);
			txtRebozado = (TextView)convertView.findViewById(R.id.txtRebozado);
			saborBar = (RatingBar)convertView.findViewById(R.id.saborBar);
			txtSabor = (TextView)convertView.findViewById(R.id.txtSabor);
			precioBar = (RatingBar)convertView.findViewById(R.id.precioBar);
			txtPrecio = (TextView)convertView.findViewById(R.id.txtPrecio);
			
			tamanioBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
					txtTamanio.setText(String.valueOf(rating));
					cachopo.setTamanio(Double.valueOf(rating));
				}
			});
			
			rellenoBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
					txtRelleno.setText(String.valueOf(rating));
					cachopo.setRelleno(Double.valueOf(rating));
				}
			});
			rebozadoBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
					txtRebozado.setText(String.valueOf(rating));
					cachopo.setRebozado(Double.valueOf(rating));
				}
			});
			saborBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
					txtSabor.setText(String.valueOf(rating));
					cachopo.setSabor(Double.valueOf(rating));
				}
			});
			precioBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
					txtPrecio.setText(String.valueOf(rating));
					cachopo.setPrecio(Double.valueOf(rating));
				}
			});

			populateDetalleCachopo(cachopo,convertView);
			
		}
		return convertView;
	}

	
	private boolean siHuboVotaciones(CachopoVO cachopo,CachopoVO cachopoBBDD) {
		boolean sihuboVotaciones=false;
		if(cachopo!=null && cachopoBBDD!=null){
			if(cachopoBBDD.getIdCachopo()==cachopo.getIdCachopo()){
				if( (cachopo.getTamanio()!=cachopoBBDD.getTamanio() )|| (cachopo.getRelleno()!=cachopoBBDD.getRelleno()) 
						|| (cachopo.getRebozado()!=cachopoBBDD.getRebozado()) || (cachopo.getSabor()!=cachopoBBDD.getSabor()) 
						|| (cachopo.getPrecio()!=cachopoBBDD.getPrecio())){
					sihuboVotaciones=true;
				}
			}
		}
		
		return sihuboVotaciones;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		
		int i = 0;
        try {
        i = children[groupPosition].length;
 
        } catch (Exception e) {
        }
        
        return i;
        
	}

	@Override
	public Object getGroup(int groupPosition) {
		CachopoVO cachopoVO= new CachopoVO();
		cachopoVO=(CachopoVO) cachopoArrayList.get(groupPosition);
		return cachopoVO;
	}

	@Override
	public int getGroupCount() {
		return cachopoArrayList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		CachopoVO cachopoVO = (CachopoVO) getGroup(groupPosition);
		if (convertView == null) {
			 LayoutInflater inflater = (LayoutInflater) contextGlobal.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		     convertView = inflater.inflate(R.layout.template_listacachopos_detalle, null);
		}
		TextView txtCachopoNombre = (TextView) convertView.findViewById(R.id.nombreCachopo);
		TextView txtPuntuacionMedia = (TextView) convertView.findViewById(R.id.txtPuntuacionMedia);
		RatingBar rtPuntuacionMediaBar  = (RatingBar) convertView.findViewById(R.id.puntuacionMediaBar);
		
		txtCachopoNombre.setText(cachopoVO.getNombre());
		rtPuntuacionMediaBar.setRating(cachopoVO.getMediaDeVotos().floatValue());
		txtPuntuacionMedia.setText(cachopoVO.getMediaDeVotos().toString());
		
		         
		return convertView;
		
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	public ListaExpandableCachoposBaseAdapter(Context context) {
		contextGlobal=context;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		 return true;
	}
	
	 /**
	  * Informa los rating bar y los text del popup de la informacion del cachopo
	  * @param context
	  * @param cachopo
	  * @param layout
	  */
	 static public void populateDetalleCachopo(CachopoVO cachopo, View layout){
		 
		 RatingBar tamanioBar = (RatingBar)layout.findViewById(R.id.tamanioBar);
		 TextView  txtTamanio = (TextView)layout.findViewById(R.id.txtTamanio);
		 RatingBar rellenoBar = (RatingBar)layout.findViewById(R.id.rellenoBar);
		 TextView  txtRelleno = (TextView)layout.findViewById(R.id.txtRelleno);
		 RatingBar rebozadoBar = (RatingBar)layout.findViewById(R.id.rebozadoBar);
		 TextView  txtRebozado = (TextView)layout.findViewById(R.id.txtRebozado);
		 RatingBar saborBar = (RatingBar)layout.findViewById(R.id.saborBar);
		 TextView  txtSabor = (TextView)layout.findViewById(R.id.txtSabor);
		 RatingBar precioBar = (RatingBar)layout.findViewById(R.id.precioBar);
		 TextView  txtPrecio = (TextView)layout.findViewById(R.id.txtPrecio);
		 
		 
		 tamanioBar.setRating(cachopo.getTamanio().floatValue());
		 txtTamanio.setText(cachopo.getTamanio().toString());
		 rellenoBar.setRating(cachopo.getRelleno().floatValue());
		 txtRelleno.setText(cachopo.getRelleno().toString());
		 rebozadoBar.setRating(cachopo.getRebozado().floatValue());
		 txtRebozado.setText(cachopo.getRebozado().toString());
		 saborBar.setRating(cachopo.getSabor().floatValue());
		 txtSabor.setText(cachopo.getSabor().toString());
		 precioBar.setRating(cachopo.getPrecio().floatValue());
		 txtPrecio.setText(cachopo.getPrecio().toString());
		 
	 }
	
}
